n = int(input())
if 1000 > n or n > 9999 :
    print(0)
    exit()
s = []
s.append(n)
n = str(n)
sp_prost = []
for i in range(4):
    s.append(int(n[i]))
    if i != 3:
        s.append(int(n[i:i+2]))
    if i != 2:
        s.append(int(n[i:i + 3]))
for i in s:
    for j in range(2, i):
        if i % j == 0:
            break
    else:
        sp_prost.append(i)

if sp_prost == []:
    print(0)
else:
    print(max(sp_prost))